package com.tycmc.signelectroniccontracts.act.read;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.logic.read.PreReadPDF;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.store.FileStore;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;

import java.io.File;

public class ActReadPDF extends ActBase implements PreReadPDF.ReadPDFInterface {

    private PreReadPDF pre;
    private String pdfUrl;
    private String save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_read_pdf);
        pre = new PreReadPDF(this, getIntent());
        pdfUrl = String.format(NetUrl.PDFREAD, getIntent().getStringExtra("contractNo"), getIntent().getStringExtra("subContractNo"));
        LogShow.i("pdf",pdfUrl);
        findViewById(R.id.tv_qianyue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TyTypenum.isQianyue = true;
                if (SharePre.getString(SharePreName.clientTypeID, "").equals("02")) {
                    pre.getQianyueLiebiao(v);
                } else {
                    pre.getCode(v);
                }
            }
        });


        setTitleText("签约");
        pre.initData(pdfUrl);
        TextView textView = (TextView) findViewById(R.id.tv_add);
        textView.setText("刷新");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pre.onrefresh(pdfUrl);
            }
        });
        findViewById(R.id.tv_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pre.yidu();
            }
        });
        findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });

    }

    @Override
    public void initbytes(byte[] urlPath) {
        findViewById(R.id.tv_yichang).setVisibility(View.GONE);

         save = FileStore.save(System.currentTimeMillis() + "", urlPath);
        File file = new File(save);
        PDFView pdfView = (PDFView) findViewById(R.id.pdfview);
        pdfView.fromFile(file)
                .defaultPage(0)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        if (page == pageCount - 1) {
                            pre.first();
                        }
                    }
                })
                .enableAnnotationRendering(true)
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {

                    }
                })
                .scrollHandle(new DefaultScrollHandle(this))
                .spacing(10) // in dp
                .onPageError(new OnPageErrorListener() {
                    @Override
                    public void onPageError(int page, Throwable t) {
                        findViewById(R.id.tv_yichang).setVisibility(View.VISIBLE);
                    }
                })
                .pageFitPolicy(FitPolicy.BOTH)
                .swipeHorizontal(false)
                .load();


//        PDFView pdfView = (PDFView) findViewById(R.id.pdfview);
//        pdfView.fromBytes(urlPath)
//                .defaultPage(0)
//                .onPageChange(new OnPageChangeListener() {
//                    @Override
//                    public void onPageChanged(int page, int pageCount) {
//                        if (page == pageCount - 1) {
//                            pre.first();
//                        }
//                    }
//                })
//                .enableAnnotationRendering(false)
//                .onLoad(new OnLoadCompleteListener() {
//                    @Override
//                    public void loadComplete(int nbPages) {
//
//                    }
//                })
////                .scrollHandle(new DefaultScrollHandle(this))
//                .spacing(0) // in dp
//                .onPageError(new OnPageErrorListener() {
//                    @Override
//                    public void onPageError(int page, Throwable t) {
//
//                    }
//                }).onError(new OnErrorListener() {
//            @Override
//            public void onError(Throwable t) {
//                findViewById(R.id.tv_yichang).setVisibility(View.VISIBLE);
//            }
//        }).pageFitPolicy(FitPolicy.BOTH)
//                .swipeHorizontal(false)
//                .load();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pre.ondeStory();
        if(!TextShow.isEmpty(save)){
            if(new File(save).exists()){
                new File(save).delete();
            }
        }

    }

    @Override
    public void daojishi(int what) {
        TextView tvQianYue = (TextView) findViewById(R.id.tv_agree);
        if (what > 0) {
            tvQianYue.setText("同意(" + what + ")");
            tvQianYue.setEnabled(false);
        } else {
            tvQianYue.setText("同意");
            tvQianYue.setEnabled(true);
        }
    }


    @Override
    public void yiqianyue(boolean isVisible) {
        findViewById(R.id.ll_yuedu).setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void yiyuedu(boolean isyueedu) {
        findViewById(R.id.ll_agree).setVisibility(isyueedu ? View.GONE : View.VISIBLE);
        findViewById(R.id.tv_qianyue).setVisibility(!isyueedu ? View.GONE : View.VISIBLE);

    }
    @Override
    public void initPDFUrl(final File file) {
        findViewById(R.id.tv_yichang).setVisibility(View.GONE);
        PDFView pdfView = (PDFView) findViewById(R.id.pdfview);
        pdfView.fromFile(file)
                .defaultPage(0)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        if (page == pageCount - 1) {
                            pre.first();
                        }
                    }
                })
                .enableAnnotationRendering(true)
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {

                    }
                })
                .scrollHandle(new DefaultScrollHandle(this))
                .spacing(10) // in dp
                .onPageError(new OnPageErrorListener() {
                    @Override
                    public void onPageError(int page, Throwable t) {
                        findViewById(R.id.tv_yichang).setVisibility(View.VISIBLE);
                    }
                })
                .pageFitPolicy(FitPolicy.BOTH)
                .swipeHorizontal(false)
                .load();

    }
    @Override
    public void onrefrsh() {
        pre.onrefresh(pdfUrl);
    }
}
